<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12" >
            <a-form-model-item label="刀具编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cuttingToolid">
              <a-input v-model="model.cuttingToolid" placeholder="请输入刀具编号" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="刀具名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cuttingToolname">
              <a-input v-model="model.cuttingToolname" placeholder="请输入刀具名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="刀具型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cuttingToolmodel">
              <a-input v-model="model.cuttingToolmodel" placeholder="请输入刀具型号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="刀具样式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cuttingToolstyle">
              <j-image-upload isMultiple  v-model="model.cuttingToolstyle" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="刀具长度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cuttingLength">
              <a-input v-model="model.cuttingLength" placeholder="请输入刀具长度" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="刀具供应商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cuttingVendor">
              <j-dict-select-tag type="list" v-model="model.cuttingVendor" dictCode="sys_vendor,vendor_name,vendor_name" placeholder="请选择刀具供应商" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="刀具负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cuttingHead">
              <j-select-user-by-dep v-model="model.cuttingHead" :multi="true" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="刀具是否使用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cuttingSwitch">
              <j-switch v-model="model.cuttingSwitch"  ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="刀具说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cuttingIllustrate">
              <j-editor v-model="model.cuttingIllustrate" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="刀具库存" :key="refKeys[0]" :forceRender="true">
        <sys-cutting-storage-form ref="sysCuttingStorageForm" @validateError="validateError" :disabled="formDisabled"></sys-cutting-storage-form>
      </a-tab-pane>
      
      <a-tab-pane tab="刀具详细状态" :key="refKeys[1]" :forceRender="true">
        <sys-cutting-specific-form ref="sysCuttingSpecificForm" @validateError="validateError" :disabled="formDisabled"></sys-cutting-specific-form>
      </a-tab-pane>
      
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import SysCuttingStorageForm from './SysCuttingStorageForm.vue'
  import SysCuttingSpecificForm from './SysCuttingSpecificForm.vue'

  export default {
    name: 'SysCuttingForm',
    mixins: [JEditableTableModelMixin],
    components: {
      SysCuttingStorageForm,
      SysCuttingSpecificForm,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['sysCuttingStorage', 'sysCuttingSpecific', ],
        tableKeys:[],
        activeKey: 'sysCuttingStorage',
        // 刀具库存
        sysCuttingStorageTable: {
          loading: false,
          dataSource: [],
          columns: [
          ]
        },
        // 刀具详细状态
        sysCuttingSpecificTable: {
          loading: false,
          dataSource: [],
          columns: [
          ]
        },
        url: {
          add: "/sysCutting/sysCutting/add",
          edit: "/sysCutting/sysCutting/edit",
          queryById: "/sysCutting/sysCutting/queryById",
          sysCuttingStorage: {
            list: '/sysCutting/sysCutting/querySysCuttingStorageByMainId'
          },
          sysCuttingSpecific: {
            list: '/sysCutting/sysCutting/querySysCuttingSpecificByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      addBefore(){
        this.$refs.sysCuttingStorageForm.clearFormData()
        this.$refs.sysCuttingSpecificForm.clearFormData()
      },
      getAllTable() {
        return new Promise(resolve => {
          resolve([]);
        })
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
          this.$refs.sysCuttingStorageForm.initFormData(this.url.sysCuttingStorage.list,this.model.id)
          this.$refs.sysCuttingSpecificForm.initFormData(this.url.sysCuttingSpecific.list,this.model.id)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
        }
      },
      //校验所有一对一子表表单
      validateSubForm(allValues){
          return new Promise((resolve,reject)=>{
            Promise.all([
                this.$refs.sysCuttingStorageForm.validate(0),
                this.$refs.sysCuttingSpecificForm.validate(1),
            ]).then(() => {
              resolve(allValues)
            }).catch(e => {
              if (e.error === VALIDATE_NO_PASSED) {
                // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
              } else {
                console.error(e)
              }
            })
          })
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          sysCuttingStorageList: this.$refs.sysCuttingStorageForm.getFormData(),
          sysCuttingSpecificList: this.$refs.sysCuttingSpecificForm.getFormData(),
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>